package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class TransactionAdapterffffffffff extends RecyclerView.Adapter<TransactionAdapterffffffffff.TransactionViewHolder> {

    private final List<Transaction> transactionList;
    private final Context context;

    public TransactionAdapterffffffffff(List<Transaction> transactionList, Context context) {
        this.transactionList = transactionList;
        this.context = context;
    }

    @NonNull
    @Override
    public TransactionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_transaction, parent, false);
        return new TransactionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TransactionViewHolder holder, int position) {
        Transaction transaction = transactionList.get(position);

        holder.tvProductName.setText(transaction.getProductName());
        holder.tvBuyerSeller.setText("买家ID: " + transaction.getBuyerId() + " | 卖家ID: " + transaction.getSellerId());
        holder.tvStatus.setText("状态: " + transaction.getStatus());
        holder.tvDate.setText("交易时间: " + transaction.getDate());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (transaction.getStatus() .equals("待支付") ) {
                    Intent intent = new Intent(context, ScanPayActivity.class);
                    context.startActivity(intent);
                } else if (transaction.getStatus() .equals( "已完成")) {
                    // 跳转到确认收货页面
                    Intent intent = new Intent(context, ConfirmReceiptActivity.class);
                    intent.putExtra("transaction_id", transaction.getTransactionId());


                    intent.putExtra("product_name", transaction.getProductName());
                    intent.putExtra("purchase_date", transaction.getDate());

                    context.startActivity(intent);

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return transactionList.size();
    }

    public static class TransactionViewHolder extends RecyclerView.ViewHolder {
        TextView tvProductName, tvBuyerSeller, tvStatus, tvDate;

        public TransactionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvProductName = itemView.findViewById(R.id.tvProductName);
            tvBuyerSeller = itemView.findViewById(R.id.tvBuyerSeller);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            tvDate = itemView.findViewById(R.id.tvDate);
        }
    }
}
